package util

import "math/rand"

func IsASCIIUpper(c byte) bool {
	return c >= 'A' && c <= 'Z'
}

func UpperLowerExchange(c byte) byte {
	return c ^ ' '
}

// 驼峰转蛇形
// isSuccessOk => is_success_ok

func CamelToSnake(s string) string {
	if len(s) == 0 {
		return ""
	}
	for i := 0; i < len(s); i++ {
		if IsASCIIUpper(s[i]) {
			s = s[:i] + "_" + string(UpperLowerExchange(s[i])) + s[i+1:]
		}
	}
	return s
}

var letterRunes = []rune("abfdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ韩🏷")

func RandStringRunes(n int) string {
	b := make([]rune, n) // len(b) = cap(b) = n
	L := len(letterRunes)
	for i := range b {
		b[i] = letterRunes[rand.Intn(L)]
	}
	return string(b)
}
